package com.vlong.town.controller.server.house;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.house.HouseInfo;
import com.vlong.town.service.house.HouseInfoService;
import com.vlong.town.util.common.TownConstant;
/**
 * 房屋出租Rest接口Controller
 * @author Assassin
 */
@RequestMapping("/rest/house")
@RestController
public class HouseInfoRestController extends   BaseController{
	@Autowired
	private  HouseInfoService houseInfoService;
	
	/**
     * 查询房屋出租信息
     * @param pageNo 当前页码
     * @param pageSize 每页显示记录数
     * @return List<HouseInfo>
     */
    @GetMapping("/list/{pageNo}/{pageSize}")
    public List<HouseInfo> list(
            @PathVariable(required=true, value="pageNo") Integer pageNo,
            @PathVariable(required=true, value="pageSize") Integer pageSize) {
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        PageHelper.orderBy("update_time DESC");
        List<HouseInfo> houseInfoList = this.houseInfoService.queryList(null, null, null,null);
        return houseInfoList;
    }
    
    
    /**
     * 小程序
     * 查询房屋出租信息
     * @param pageNo 当前页码
     * @param pageSize 每页显示记录数
     * @return List<HouseInfo>
     */
    @GetMapping("/listXcx")
    public List<HouseInfo> listXcx() {
        List<HouseInfo> houseInfoList = this.houseInfoService.queryList(null, null, null,null);
        for (HouseInfo houseInfo : houseInfoList) {
        	Long createTime = houseInfo.getCreateTime();
        	SimpleDateFormat  sdf=new  SimpleDateFormat("yyyy-MM-dd");
        	Date date = new  Date(createTime);
        	String format = sdf.format(date);
        	houseInfo.setCreateDate(format);
		}
        return houseInfoList;
    }
    
    
    /**
     * 查询房屋出租详情
     * @param id 房屋出租id
     * @return HouseInfo
     */
    @GetMapping("/detail/{id}")
    public HouseInfo detail(@PathVariable(required=true, value="id") Long id) {
    	return houseInfoService.queryById(id);
    }
    
    @ResponseBody
    @RequestMapping("/addHouseInfo")
    public  DataMessage  addHouseInfo(
    		@RequestParam(required=false, value="wxId") String wxId,
    		@RequestParam(required=false, value="title") String title,
    		@RequestParam(required=false, value="content") String content,
    		@RequestParam(required=false, value="desc") String desc,
    		@RequestParam(required=false, value="thumbnail") String thumbnail,
    		@RequestParam(required=false, value="acreage") String acreage,
    		@RequestParam(required=false, value="address") String address,
    		@RequestParam(required=false, value="monthlyRent") Long monthlyRent
    		){
    	DataMessage  dm=new  DataMessage();
    	if(!validateStringProperty(title, 2, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("房租标题参数错误");
    	}else  if(!validateStringProperty(content, 2, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("房租内容参数错误");
    	}else  if(!validateStringProperty(desc, 2, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("房租描述参数错误");
    	}else  if(!validateStringProperty(acreage, 2, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("房租面积参数错误");
    	}else  if(!validateStringProperty(address, 2, 128)){
    		dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
    		dm.setMessage("房租位置参数错误");
    	}else{
    		houseInfoService.add(null, title, acreage, address, monthlyRent, content, desc, thumbnail,wxId);
    		dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
    		dm.setMessage("添加成功");
    	}
    	return dm;
    }
    
    
}
